package com.example.app.ui.Me;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.example.app.LoginActivity;
import com.example.app.R;
import com.example.app.Websocket.APP;
import com.example.app.databinding.FragmentMeBinding;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MeFragment extends Fragment {
    private FragmentMeBinding binding;
    @SuppressLint("SetTextI18n")
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        binding = FragmentMeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        ActionBar supportActionBar = activity.getSupportActionBar();
        if (supportActionBar!=null){
            View customView = supportActionBar.getCustomView();
            TextView textViewTitle = customView.findViewById(R.id.textView_title);
            textViewTitle.setText("我");
        }
        ListView listView = root.findViewById(R.id.melv);
        View headerView = inflater.inflate(R.layout.user_info_header, listView, false);
        TextView nicknameTV = headerView.findViewById(R.id.nickname_tv);
        TextView userIdTV = headerView.findViewById(R.id.user_id_tv);
        SharedPreferences sharedPreferences = requireContext().getSharedPreferences("user_credentials", Context.MODE_PRIVATE);
        String username = sharedPreferences.getString("username", "微信用户");
        String nickname = sharedPreferences.getString("nickname", "微信用户");

        nicknameTV.setText(nickname);
        userIdTV.setText("X信号: " + (username != null ? username : "未设置"));
        listView.addHeaderView(headerView);
        List<Map<String, Object>> menuItems = new ArrayList<>();
        addMenuItem(menuItems, R.drawable.manager, "设置");
        addMenuItem(menuItems, R.drawable.favors, "收藏");
        addMenuItem(menuItems, R.drawable.post, "朋友圈");
        addMenuItem(menuItems, R.drawable.doctor, "医生信息");
        addMenuItem(menuItems, R.drawable.logout, "退出登录");
        MenuAdapter adapter = new MenuAdapter(requireContext(), menuItems);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener((parent, view, position, id) -> {
            int headerCount = listView.getHeaderViewsCount();
            int itemPosition = position - headerCount;
            if (itemPosition >= 0 && itemPosition < menuItems.size()) {
                Map<String, Object> item = menuItems.get(itemPosition);
                String title = (String) item.get("title");
                if ("退出登录".equals(title)) {
                    // 清除用户凭证
                    sharedPreferences.edit()
                            .clear() // 或移除具体字段：.remove("username").remove("password")
                            .apply();
                    try {
                        APP.getDataWebSocketManager().disconnect();
                    }catch (Exception e){
                        System.out.println("-------------------MeFragment-------------------");
                    }
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                    requireActivity().finish();
                }
            }
        });
        return root;
    }
    private void addMenuItem(List<Map<String, Object>> list, int iconRes, String title) {
        Map<String, Object> item = new HashMap<>();
        item.put("icon", iconRes);
        item.put("title", title);
        list.add(item);
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}